package top.lidee.common.service.twice.third.ali.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.FileItem;
import com.alipay.api.diagnosis.DiagnosisUtils;
import com.alipay.api.request.AntMerchantExpandIndirectImageUploadRequest;
import com.alipay.api.response.AntMerchantExpandIndirectImageUploadResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import top.lidee.common.service.twice.third.ali.IndirectImageUploadService;

import java.util.Arrays;
import java.util.Base64;

/**
 * @author 张一恒
 * @version 1.0
 * @description: ant.merchant.expand.indirect.image.upload(图片上传)
 * @date 2024/9/29 09:23
 */
@Service("indirectImageUploadService")
@Slf4j
public class IndirectImageUploadServiceImpl implements IndirectImageUploadService {
    // 初始化SDK
    @Autowired
    private AlipayClient alipayClient;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * @param imageType    图片格式，支持格式：bmp、jpg、jpeg、png、gif.
     * @param imageContent 图片二进制字节流，最大为10M
     * @return java.lang.String
     * @description 图片上传
     * @author zhangyiheng
     * @date 2024/9/29 09:24
     */
    @Override
    public String load(String imageType, byte[] imageContent, String fileName) {

        try {
            // 构造请求参数以调用接口
            AntMerchantExpandIndirectImageUploadRequest request = new AntMerchantExpandIndirectImageUploadRequest();

            // 设置图片/视频格式
            request.setImageType(imageType);

            // 设置图片内容
            FileItem imageContentFile = new FileItem(fileName, imageContent);
            request.setImageContent(imageContentFile);

            AntMerchantExpandIndirectImageUploadResponse response = alipayClient.execute(request);
            System.out.println(response.getBody());

            if (response.isSuccess()) {
                System.out.println("调用成功");
            } else {
                System.out.println("调用失败");
                // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
                String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
                System.out.println(diagnosisUrl);
            }
            return response.getBody();
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public byte[] urlOfBytes(String imageUrl, MediaType mediaType) {
        // 设置HTTP头部信息
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(mediaType); // 假设图片类型为JPEG，根据实际情况调整


        // 发送HTTP请求获取图片数据流
        ResponseEntity<byte[]> imageResponse = restTemplate.exchange(imageUrl, HttpMethod.GET, new HttpEntity<>(headers), byte[].class);

        // 将图片数据流写入响应输出流
        if (imageResponse.getStatusCode() == HttpStatus.OK && imageResponse.getBody() != null) {
            return imageResponse.getBody(); // 将图片数据写入响应输出流
        } else {
            log.error("alipay图片获取失败：{}", HttpStatus.NOT_FOUND.value());
            return null; // 处理请求失败的情况
        }
    }

    public static void main(String[] args) {

    }
}
